package com.cyanite.playlv2.widget;

import com.cyanite.playlv2.util.RemoteResourceManager;
import com.cyanite.playlv2api.PlayLv2Api;
import com.cyanite.playlv2api.types.User;
import com.cyanite.playlv2.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;
import java.util.HashSet;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;

public class FriendListAdapter extends BaseGroupAdapter<User>
    implements ObservableAdapter {

    private LayoutInflater mInflater;
    private int mLayoutToInflate;
    private RemoteResourceManager mRrm;
    private RemoteResourceManagerObserver mResourcesObserver;
    private Handler mHandler = new Handler();

    private Set<String> mLaunchedPhotoFetches;

    public FriendListAdapter(Context context, RemoteResourceManager rrm) {
        super(context);
        mInflater = LayoutInflater.from(context);
        mLayoutToInflate = R.layout.friend_list_item;
        mRrm = rrm;
        mResourcesObserver = new RemoteResourceManagerObserver();
        mLaunchedPhotoFetches = new HashSet<String>();

        mRrm.addObserver(mResourcesObserver);
    }

    public void removeObserver() {
        mHandler.removeCallbacks(mUpdatePhoto);
        mRrm.deleteObserver(mResourcesObserver);
    }

    public FriendListAdapter(Context context, int layoutResource) {
        super(context);
        mInflater = LayoutInflater.from(context);
        mLayoutToInflate = layoutResource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(mLayoutToInflate, null);
            holder = new ViewHolder();
            holder.photo = (ImageView) convertView.findViewById(R.id.friendListItemPhoto);
            holder.name = (TextView) convertView.findViewById(R.id.friendListItemName);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        User user = (User) getItem(position);
        Uri photoUri = Uri.parse(user.getPhoto());
        try {
            Bitmap bitmap = BitmapFactory.decodeStream(mRrm.getInputStream(photoUri));
            holder.photo.setImageBitmap(bitmap);
        } catch (IOException e) {
            if (PlayLv2Api.MALE.equals(user.getGender())) {
                holder.photo.setImageResource(R.drawable.blank_boy);
            } else {
                holder.photo.setImageResource(R.drawable.blank_girl);
            }

            if (!mLaunchedPhotoFetches.contains(user.getId())) {
                mLaunchedPhotoFetches.add(user.getId());
                mRrm.request(photoUri);
            }
        }

        holder.name.setText(user.getUserName());

        return convertView;
    }

    public void removeItem(int position) throws IndexOutOfBoundsException {
        group.remove(position);
        notifyDataSetInvalidated();
    }

    private class RemoteResourceManagerObserver implements Observer {
        @Override
        public void update(Observable observable, Object data) {
            mHandler.post(mUpdatePhoto);
        }
    }

    private Runnable mUpdatePhoto = new Runnable() {
        @Override
        public void run() {
            notifyDataSetChanged();
        }
    };

    static class ViewHolder {
        ImageView photo;
        TextView name;
    }
}
